#ifndef _TIME_MGR_H_
#define _TIME_MGR_H_

#include "compiler.h"

#define CHECK_TIME_VALUE                10

#define LED_NEXT_TIME_SLICE             100
#define LED_LAST_LED_CONTINUE_TIME      3000
#define LED_FLICK_TIME_VALUE            500
#define LED_CONTINUE_TIME               30000
#define LED_1S_SLICE                    1000

#define WDT_RESET_TIME                 15000
#ifdef USED_J_TAG	
#define SLEEP_TIME_SLICE_LONG          3600000
#define SLEEP_TIME_SLICE_SHORT         3600000
#else    
#define SLEEP_TIME_SLICE_LONG          180000
#define SLEEP_TIME_SLICE_SHORT         7000
#endif
#define STANDBY_TIME_SLICE             20000
#define APP_LAUNCH_TIME_SLICE          90000

#define CHECK_SLICE                     0
#define LED_CHARGE_SLICE                1
#define LED_NOCHARGE_SLICE              2
#define LED_FLICK_SLICE                 3                       
#define RE_PC_SLICE                     4
#define SLEEP_SLICE                     5 
#define DELAY_SLEEP_SLICE               6
#define BUTTON_PRESSED_SLICE            7
#define VBUSIN_DETECT_SLICE             8
#define SWITCH_DETECT_SLICE             9
#define CUR_DETECT_SLICE                10
#define STANDBY_SLICE                   11
#define TEMP_TIME_SLICE                 12
#define APP_LAUNCH_SLICE                13

typedef struct
{
    bool        b_valid;
    uint32_t    start_line;
}time_slice_t;

extern uint32_t system_ms_tick;
extern time_slice_t time_slice_vector[14];

#define get_system_time() (system_ms_tick)
void start_system_timer(uint32_t cpu_hz);
void system_timer_interrupt(void);
void stop_system_timer(void);

void start_time_slice(uint8_t idx);
#define stop_time_slice(a) time_slice_vector[a].b_valid = false
void reset_time_slice(uint8_t idx);
bool Is_time_slice_reach(uint8_t idx, uint32_t slice_value);
#define Is_time_slice_valid(a) (time_slice_vector[a].b_valid)

#define start_check_timer() start_time_slice(CHECK_SLICE)
#define stop_check_timer()  stop_time_slice(CHECK_SLICE)
#define reset_check_timer() reset_time_slice(CHECK_SLICE)
#define Is_check_time_over() (Is_time_slice_reach(CHECK_SLICE, CHECK_TIME_VALUE))

void start_led_timer(void);

#define start_charge_led_timer() start_time_slice(LED_CHARGE_SLICE)
#define stop_charge_led_timer() stop_time_slice(LED_CHARGE_SLICE)
#define Is_charge_led_timer_valid() Is_time_slice_valid(LED_CHARGE_SLICE)
#define Is_charge_led_time_over() (Is_time_slice_reach(LED_CHARGE_SLICE, LED_CONTINUE_TIME))
#define Is_charge_led_start_over() (Is_time_slice_reach(LED_CHARGE_SLICE, LED_1S_SLICE))

#define start_next_flick() start_time_slice(LED_FLICK_SLICE)
#define Is_flick_time_over() (Is_time_slice_reach(LED_FLICK_SLICE, LED_FLICK_TIME_VALUE))
#define stop_next_flick() stop_time_slice(LED_FLICK_SLICE)

#define start_nocharge_led_timer() start_time_slice(LED_NOCHARGE_SLICE)
#define stop_nocharge_led_timer() stop_time_slice(LED_NOCHARGE_SLICE)
#define Is_nocharge_led_timer_valid() Is_time_slice_valid(LED_NOCHARGE_SLICE)
#define Is_nocharge_led_time_over(a) (Is_time_slice_reach(LED_NOCHARGE_SLICE, a))
#define reset_nocharge_led_timer() reset_time_slice(LED_NOCHARGE_SLICE)

#define start_sleep_timer()     start_time_slice(SLEEP_SLICE)
#define stop_sleep_timer()      stop_time_slice(SLEEP_SLICE)
//#define Is_sleep_time_over()       (Is_time_slice_reach(SLEEP_SLICE, SLEEP_TIME_SLICE))
bool Is_sleep_time_over(void);
#define reset_sleep_timer()     reset_time_slice(SLEEP_SLICE)

#define start_delay_timer(void) start_time_slice(DELAY_SLEEP_SLICE)
#define Is_delay_time_over(a)      (Is_time_slice_reach(DELAY_SLEEP_SLICE, a))
#define stop_delay_timer(void)  stop_time_slice(DELAY_SLEEP_SLICE) 

#define start_re_pc_timer() start_time_slice(RE_PC_SLICE)
#define Is_re_pc_time_over() (Is_time_slice_reach(RE_PC_SLICE, 3000))
#define stop_re_pc_timer() stop_time_slice(RE_PC_SLICE)

#define start_temp_timer()     start_time_slice(TEMP_TIME_SLICE)
#define stop_temp_timer()      stop_time_slice(TEMP_TIME_SLICE)
#define Is_temp_time_over(a)       (Is_time_slice_reach(TEMP_TIME_SLICE, a))
#define reset_temp_timer()     reset_time_slice(TEMP_TIME_SLICE)

#define start_pressed_timer()     start_time_slice(BUTTON_PRESSED_SLICE)
#define stop_pressed_timer()      stop_time_slice(BUTTON_PRESSED_SLICE)
#define Is_pressed_time_over(a)       (Is_time_slice_reach(BUTTON_PRESSED_SLICE, a))
#define reset_pressed_timer()     reset_time_slice(BUTTON_PRESSED_SLICE)

#define start_vbusin_timer()     start_time_slice(VBUSIN_DETECT_SLICE)
#define stop_vbusin_timer()      stop_time_slice(VBUSIN_DETECT_SLICE)
#define Is_vbusin_time_over(a)       (Is_time_slice_reach(VBUSIN_DETECT_SLICE, a))
#define reset_vbusin_timer()     reset_time_slice(VBUSIN_DETECT_SLICE)

#define start_switch_timer()     start_time_slice(SWITCH_DETECT_SLICE)
#define stop_switch_timer()      stop_time_slice(SWITCH_DETECT_SLICE)
#define Is_switch_time_over(a)       (Is_time_slice_reach(SWITCH_DETECT_SLICE, a))
#define reset_switch_timer()     reset_time_slice(SWITCH_DETECT_SLICE)

#define start_cur_timer()     start_time_slice(CUR_DETECT_SLICE)
#define stop_cur_timer()      stop_time_slice(CUR_DETECT_SLICE)
#define Is_cur_time_over(a)       (Is_time_slice_reach(CUR_DETECT_SLICE, a))
#define reset_cur_timer()     reset_time_slice(CUR_DETECT_SLICE)

#define start_standby_timer()     start_time_slice(STANDBY_SLICE)
#define stop_standby_timer()      stop_time_slice(STANDBY_SLICE)
#define Is_standby_time_over()       (Is_time_slice_reach(STANDBY_SLICE, STANDBY_TIME_SLICE))
#define reset_standby_timer()     reset_time_slice(STANDBY_SLICE)

#define start_applaunch_timer()     start_time_slice(APP_LAUNCH_SLICE)
#define stop_applaunch_timer()      stop_time_slice(APP_LAUNCH_SLICE)
#define Is_applaunch_time_over()      (Is_time_slice_reach(APP_LAUNCH_SLICE, APP_LAUNCH_TIME_SLICE))
#define reset_applaunch_timer()     reset_time_slice(APP_LAUNCH_SLICE)

#endif
